package cn.geminis.everydata.service.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.ObjectUtils;

import javax.persistence.*;
import java.util.UUID;

/**
 * @author Allen
 */
@Entity
@Getter
@Setter
public class Category {

    @Id
    @Column(length = 36)
    protected String id;

    @Column(nullable = false, length = 128)
    protected String name;

    @Column(nullable = false)
    private String pathId;

    @Column(length = 36)
    private String parentId;

    @Column(nullable = false)
    private int showOrder;

    @Transient
    @JsonProperty
    private String parentPath;

    @PrePersist
    void setId() {
        if (ObjectUtils.isEmpty(id)) {
            id = UUID.randomUUID().toString();
        }
        if (ObjectUtils.isEmpty(this.parentPath)) {
            throw new RuntimeException("需要设置上级ID路径");
        }
        this.pathId = this.parentPath + this.getId() + "/";
    }
}
